import { Dialog } from '@alifd/next';
import { store, history } from '../index';


function handlerServiceError(model) {
    if(model.code == 700) {
        //登录过期、token失效
        showReloginAlert();
    }
    throw new Error(model.message || '未定义错误信息')
}

function showReloginAlert() {
    Dialog.alert({
        title: '温馨提示',
        content: '请先登录！',
        onOk: () => {history.replace('/user/login')}
    });
}

const getKeyPathMenuData = (menuData) => getFlatMenuData(menuData);

/**
 * 构造键值对 Menu 数据
 * @param menus 完整的menu数据
 */
function getFlatMenuData(menus) {
    let keys = {};
    menus.forEach(item => {
      if (item.children) {
        keys[item.path] = {...item};
        keys = {...keys, ...getFlatMenuData(item.children)};
      } else {
        keys[item.path] = {...item};
      }
    });
    return keys;
}

export {
    handlerServiceError,
    showReloginAlert,
    getKeyPathMenuData,
}